package com.gx.smart.construction.ui.elevator.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.casic.gx.grpc.common.ComQuery;
import com.casic.gx.grpc.common.ComResp;
import com.gx.smart.common.util.AppConfig;
import com.gx.smart.construction.ui.common.viewmodel.CommonViewModel;
import com.gx.smart.lib.http.api.ConstructionAppLiftInfoProviderService;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.ConstructionAppLiftInfoDto;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.ConstructionAppLiftInfoQueryResponse;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.GenerateOrCodeResponse;

import java.util.List;

/**
 * @author xiaosy
 * @create 2020/8/7
 * @Describe
 **/
public class ElevatorControlViewModel extends CommonViewModel {
    public MutableLiveData<List<ConstructionAppLiftInfoDto>> liftList = new MutableLiveData<>();
    public MutableLiveData<String> qrCode = new MutableLiveData<>();

    public ElevatorControlViewModel(@NonNull Application application) {
        super(application);
    }

    public static final int MAX_ROOM_NUM = 10000;

    public LiveData<List<ConstructionAppLiftInfoDto>> liftList(int currentPage, String unit) {
        ComQuery comQuery = ComQuery
                .newBuilder()
                .setPageSize(MAX_ROOM_NUM)
                .setPage(currentPage)
                .build();
        ConstructionAppLiftInfoProviderService.getInstance().queryLiftInfo(comQuery, unit, new CallBack<ConstructionAppLiftInfoQueryResponse>() {

            @Override
            public void callBack(ConstructionAppLiftInfoQueryResponse result) {
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    liftList.setValue(result.getContentList());
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });
        return liftList;
    }


    public LiveData<String> createQRCode(String lifeId) {
        int tenantNo = SPUtils.getInstance().getInt(AppConfig.SYS_TENANT_NO, 0);
        ConstructionAppLiftInfoProviderService.getInstance().generateOrCode(lifeId, tenantNo, new CallBack<GenerateOrCodeResponse>() {

            @Override
            public void callBack(GenerateOrCodeResponse result) {
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    qrCode.setValue(result.getOrCodeStr());
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });

        return qrCode;
    }
}
